package com.tencent.FaceTrackFaceu;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;

import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Rect;

import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.SurfaceView;
import android.util.AttributeSet;

import com.tencent.View.MyAdapter;
import com.tencent.youtufacetrack.YoutuFaceTrack;

import java.util.LinkedHashMap;

public class DrawView extends SurfaceView
{
    private Paint p = new Paint();
    private Paint p2 = new Paint();
    private float points[] = null;
    private float scale = 1;
    private int mTotalWidth, mTotalHeight;
    private int eyeRight_width;
    private int eyeRight_height;
    private Rect srcRect_eyeRight;
    private Rect desRect_eyeRight;
    private Bitmap bitmap_eyeRight;
    private Rect desRect_eyeLeft;
    int i;
    private Bitmap bitmap_eyeLeft;
    private int eyeLeft_width;
    private int eyeLeft_height;
    private Rect srcRect_eyeLeft;
    private Bitmap bitmap_mouth;
    private int mouthWidth;
    private int mouthHeight;
    private Rect srcRect_mouth;
    private Rect desRect_mouth;
    private BitmapDrawable eye_right_drawable;
    private BitmapDrawable eye_left_drawable;
    private Bitmap bitmap_eyeShadow;
    private int eyeShadow_width;
    private int eyeShadow_height;
    private Rect srcRect_eyeShadow;
    private LinkedHashMap linkedHashMap;

    public DrawView(Context context, AttributeSet attrs)
    {
        super(context, attrs);

//         p.setFilterBitmap(true);
        p.setARGB(255, 0, 255, 0);
//        p.setColor(Color.parseColor("#d3217b"));
        p.setStrokeWidth(3);

        p2.setColor(Color.parseColor("#d4237a"));
        p2.setStrokeWidth(3);
        // This call is necessary, or else the
        // draw method will not be called.
        setWillNotDraw(false);
        linkedHashMap = new LinkedHashMap();
        //初始化图片
//        initBitmap();
    }

    public void initBitmap(int[] imageviews, int position) {
        Log.e("initBitmap", "来到了initBitmap方法");
        //眼睛
        if (position < 20) {
            if (position % 2 == 0) {
                eye_right_drawable = (BitmapDrawable) getResources().getDrawable(imageviews[position]);
                eye_left_drawable = (BitmapDrawable) getResources().getDrawable(imageviews[position + 1]);
                Log.e("initBitmap", "imageviews[position] :    " + imageviews[position] );
                Log.e("initBitmap", "imageviews[position + 1] :    " + imageviews[position + 1] );
            } else if (position % 2 != 0) {
                eye_right_drawable = (BitmapDrawable) getResources().getDrawable(imageviews[position - 1]);
                eye_left_drawable = (BitmapDrawable) getResources().getDrawable(imageviews[position ]);
            }
            bitmap_eyeRight = eye_right_drawable.getBitmap();
            eyeRight_width = bitmap_eyeRight.getWidth();
            eyeRight_height = bitmap_eyeRight.getHeight();

            bitmap_eyeLeft = eye_left_drawable.getBitmap();
            eyeLeft_width = bitmap_eyeLeft.getWidth();
            eyeLeft_height = bitmap_eyeLeft.getHeight();

//            linkedHashMap.put(position, eye_right_drawable);
        }

        //眼影
        if (position>= 20 && position<= 22) {
            BitmapDrawable eyeShadow = (BitmapDrawable) getResources().getDrawable(imageviews[position]);
            bitmap_eyeShadow = eyeShadow.getBitmap();
            eyeShadow_width = bitmap_eyeShadow.getWidth();
            eyeShadow_height = bitmap_eyeShadow.getHeight();
        }

        //嘴唇
        if (position>=28 && position <= 33) {
            BitmapDrawable mouth = (BitmapDrawable) getResources().getDrawable(imageviews[position]);
            bitmap_mouth = mouth.getBitmap();
            mouthWidth = bitmap_mouth.getWidth();
            mouthHeight = bitmap_mouth.getHeight();
        }


        //设置好图片显示多少与比例
        srcRect_eyeRight = new Rect(0, 0, eyeRight_width, eyeRight_height);
        srcRect_eyeLeft = new Rect(0, 0, eyeLeft_width, eyeLeft_height);
        srcRect_mouth = new Rect(0, 0, mouthWidth, mouthHeight);
        srcRect_eyeShadow = new Rect(0, 0, eyeShadow_width, eyeShadow_height);

//            Log.e("initBitmap", "bitmap.getWidth :    " + bitmapWidth  + "    bitmap.getHeight() :    " + bitmapHeight);
//            Log.e("initBitmap",  "bitmap2.getWidth() :    " + bitmapWidth2  + "    bitmap2.getHeight() :    " + bitmapHeight2);
//        Log.e("initBitmap", "bitmapWidth:       " + bitmapWidth + "bitmapHeight:   " + bitmapHeight);
    }

    public static float[] addAll(float[] array1, float[] array2)
    {
        Log.d("FaceTrack", "来到 DrawView 的 addAll 方法");
        if (array1 == null) {
            return array2.clone();
        }
        else if (array2 == null) {
            return array1.clone();
        }
        float[] joinedArray = new float[array1.length + array2.length];
        System.arraycopy(array1, 0, joinedArray, 0, array1.length);
        System.arraycopy(array2, 0, joinedArray, array1.length, array2.length);
        return joinedArray;
    }


    //for native call
   // public void setPoints(NativeInterface.FaceStatus[] facestatus, int w, int h)
    //for java call
    public void setPoints(YoutuFaceTrack.FaceStatus[] facestatus, int w, int h)
    {
        Log.e("method", "来到 DrawView 的 setPoints 方法");
        points = null;

        if (facestatus != null && facestatus.length!=0)
        {
            for (int i=0; i<facestatus.length; i++)
            {

//                Log.e("numOfFaceStatus", String.valueOf(facestatus.length));
                points = addAll(points, facestatus[i].xys);

            }
            if (scale == 1)
            {
                scale = getWidth() / (float)w;
                if (scale > getHeight() / (float)h)
                {
                scale = getHeight() / (float)h;
                }
            }
        }

        postInvalidate();
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        Log.e("method", "来到了onMesure方法");
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    /*左眼宽：数组【40】 - 数组【32】
    左眼高：数组【45】 - 数组【37】*/
    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        Log.e("onSizeChanged", "onSizeChanged , w = "+w+" , h = "+h+" , mBitWidth = "+oldw+" , mBitHeight = "+oldh);
        mTotalHeight = h;
        mTotalWidth = w;

        /*desRect = new Rect(0, 0,i ++,i ++);
        Log.e("onSizeChanged", "i =   "+i);*/
    }

    @Override
    protected void onDraw(Canvas canvas)
    {
        if (points != null)
        {
            canvas.scale(scale, scale);
            //画上脸部定位绿色点
            canvas.drawPoints(points, p);
            //保存canvas
            canvas.save();


//在指点位置贴上指定大小的图片，眼睛18个====================================================================================
            //眼睛
            if (bitmap_eyeRight != null && bitmap_eyeLeft != null) {
                Log.e("initBitmap", "进入绘画眼睛的方法");
                //右眼：X:-(176-32) (40 - 176) Y:-(177 - 45) (37 -177 )
//            desRect = new Rect((int)points[32] - 10, (int)points[45] - 15,(int)points[40] + 20, (int)points[37] + 10);

                desRect_eyeRight = new Rect(-(int)(points[176]-points[32] + 10),  - (int)(points[177] - points[45] + 10),(int) (points[40]-points[176] + 10),  (int)(points[37]-points[177] + 10));
                canvas.translate(points[176], points[177]);
                canvas.rotate((float) Math.toDegrees(Math.atan( (points[41] -points[33] )/(points[40] - points[32] ))));
                canvas.drawBitmap(bitmap_eyeRight,srcRect_eyeRight,desRect_eyeRight,null);


                //左眼X:-(178-56) (48 - 178) Y:-(179 - 61) (53 -179 )
//            desRect2 = new Rect((int)points[56] - 10, (int)points[61] - 10,(int)points[48] + 10, (int)points[53] + 10);
                desRect_eyeLeft = new Rect(-(int)(points[178]-points[56] + 10),  - (int)(points[179] - points[61] + 10),(int) (points[48]-points[178] + 10),  (int)(points[53]-points[179] + 10));
                //获取保存时canvas状态
                canvas.restore();
                canvas.save();
                canvas.translate(points[178], points[179]);
                canvas.rotate((float) Math.toDegrees(Math.atan( (points[49] -points[57] )/(points[48] - points[56] ))));
                canvas.drawBitmap(bitmap_eyeLeft,srcRect_eyeLeft,desRect_eyeLeft,null);

            }

            //嘴唇
            if (bitmap_mouth != null) {
                //红唇X:-(x3-90) (102 - x3) Y:-(y3 - 109) (97 -y3 )
                float x3 = (points[118] + points[128]) / 2;
                float y3 = (points[119] + points[129]) /2;
                float y31 = (points[111] + points[109]) /2;
                desRect_mouth = new Rect(-(int)(x3-points[90] ),  - (int)(y3 - y31),(int) (points[102]-x3 ),  (int)(points[97]-y3 ));
                //获取保存时canvas状态
                canvas.restore();
                canvas.save();
                canvas.translate( x3, y3 );
                canvas.rotate((float) Math.toDegrees(Math.atan( (points[49] -points[57] )/(points[48] - points[56] ))));
                canvas.drawBitmap(bitmap_mouth,srcRect_mouth,desRect_mouth,null);
            }

            //眼影
            if (bitmap_eyeShadow != null) {
                //x:-(42 - 32)  (40 - 42)   Y:-(10 ) (10 )
                Rect desRect_eyeShadow = new Rect(-(int) (points[42] - points[32] + 5), (-5), (int) (points[40] - points[42] ), (10));
                //获取保存时canvas状态
                canvas.restore();
                canvas.save();
                canvas.translate(points[42], points[43] );
                canvas.rotate((float) Math.toDegrees(Math.atan( (points[49] -points[57] )/(points[48] - points[56] ))));
                canvas.drawBitmap(bitmap_eyeShadow,srcRect_eyeShadow,desRect_eyeShadow,null);
            }
//在指点位置贴上指定大小的图片===============================================================================================

        }
    }
}
